import wepy from 'wepy'

export default class Corner extends wepy.mixin {
  /**
   * 有效时间
   */
  verifyTime (corner) {
    if (!corner.beginTime && !corner.endTime) return true
    else if (corner.beginTime && corner.endTime) return !!(new Date() - new Date(corner.beginTime.replace(/-/g, '/')) > 0 && new Date() - new Date(corner.endTime.replace(/-/g, '/')) < 0)
    else console.warn('开始和结束时间必须都要传入')
  }
  /**
   * 图标类型
   */
  currentIcon (corner, customTpye) {
    const [_new, _hot] = ['./../assets/img/corner-new.svg', './../assets/img/corner-hot.svg']
    switch (corner.style) {
      case 'NEW':
        return _new

      case 'HOT':
        return _hot

      case 'CUSTOM':
        if (customTpye === 'COVER') return corner.customCoverUrl
        else if (customTpye === 'MAIN') return corner.customMainUrl
    }
  }
  /**
   * 获取uri参数值
   *
   * @param {String} uri 路径资源
   * @param {String} query 查询具体的某个参数
   */
  searchURI (uri, query) {
    if (!uri.includes('?')) return ''
    let params = uri.split('?')[1]
    let regex = new RegExp('[?|&|=]' + query + '=([^&#]*)')
    let results = regex.exec('?' + params)
    return results == null ? '' : results[1]
  }
}
